python学习 您所在的位置:网站首页 csv转shp arcgis python学习

python学习

2023-10-14 09:06| 来源: 网络整理| 查看: 265

python——csv文件转换为shp 转载自https://blog.csdn.net/qq_23926575/article/details/82316965

学地信的经常会有这样的需求,即将csv格式的文件转换为shapefile格式加载到arcMap中进行显示与分析,arcMap本身提供了这样的功能,即

文件->添加数据->添加XY数据:选择文件路径,指定X字段(longitude)、Y字段(latitude)即可,默认坐标系为WGS84坐标系。

arcMap的这个功能非常方便,但因为我的数据量较大,有接近100万条数据,故通过代码进行格式转换效率更高点。 下面的代码参考如下链接:Using pyshp to convert .csv file to .shp? 需要用到pyshp模块、csv模块、codecs模块,pip install 安装即可。 下面贴上代码: 目前只能转换point类型数据

#-*-coding:utf-8-*- import shapefile as shp import csv import codecs import os def trans_point(folder, fn, idlng, idlat, delimiter=','): # create a point shapefile output_shp = shp.Writer(shp.POINT) # for every record there must be a corresponding geometry. output_shp.autoBalance = 1 # create the field names and data type for each.you can omit fields here # output_shp.field('id','N') # number output_shp.field('longitude', 'F', 10, 8) # float output_shp.field('latitude', 'F', 10, 8) # float output_shp.field('locname','C',100) # string, max-length # access the CSV file with codecs.open(folder + fn, 'rb', 'utf-8') as csvfile: reader = csv.reader(csvfile, delimiter=delimiter) # skip the header next(reader, None) #loop through each of the rows and assign the attributes to variables for row in reader: # idx = row[0] locname= row[1] lng= float(row[idlng]) lat = float(row[idlat]) print lng, lat # create the point geometry output_shp.point(lng, lat) # add attribute data output_shp.record(lng, lat, locname) output_shp.save(folder + "%s.shp"%fn.split('.')[0]) # save the Shapefile if __name__ == '__main__': folder = 'C:\Users\MaMQ\Desktop' + os.sep fn = 'test.csv' trans_point(folder, fn, 2, 3)

示例数据

idlocnamexy1Haryana, Haryana, India76.980629.61612Mumbai, Maharashtra, India72.825818.975

结果 这里写图片描述

参考资料 [1] Using pyshp to convert .csv file to .shp?



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

      专题文章
        CopyRight 2018-2019 实验室设备网 版权所有